﻿using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

namespace RahulBabar.SharePoint.Util.UlsLogger
{
    /// <summary>
    /// Different Log Categories for Application
    /// </summary>
    public enum LogCategories
    {        
        MY_APPLICATION_INFO,
        MY_APPLICATION_WARNING,
        MY_APPLICATION_ERROR
    }

    /// <summary>
    /// Custom Application Logger Service
    /// </summary>
    public class LoggingService : SPDiagnosticsServiceBase
    {
        private static string PRODUCT_NAME = "MyApplication";
        private LoggingService() : base("Custom USL Logging Service", SPFarm.Local) { }

        private static LoggingService logSerivce;
        public static LoggingService LogSerivce
        {
            get
            {
                if (logSerivce == null)
                {
                    logSerivce = new LoggingService();
                }
                return logSerivce;
            }
        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea> {
              new SPDiagnosticsArea(PRODUCT_NAME, new List<SPDiagnosticsCategory> {
                  new SPDiagnosticsCategory(LogCategories.MY_APPLICATION_INFO.ToString(), TraceSeverity.Medium, EventSeverity.Information),
                  new SPDiagnosticsCategory(LogCategories.MY_APPLICATION_WARNING.ToString(), TraceSeverity.High, EventSeverity.Warning),
                  new SPDiagnosticsCategory(LogCategories.MY_APPLICATION_ERROR.ToString(), TraceSeverity.Unexpected, EventSeverity.Error)                  
              })
              };
            return areas;
        }

        /// <summary>
        /// Logs meesage in SharePoint ULS log files with given category
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="message"></param>
        /// <param name="category"></param>
        public static void LogMessage(string className, string methodName, string message, LogCategories category)
        {
            SPDiagnosticsCategory logCatgory = LoggingService.LogSerivce.Areas[PRODUCT_NAME].Categories[category.ToString()];
            switch (category)
            {                
                case LogCategories.MY_APPLICATION_INFO:
                    LoggingService.LogSerivce.WriteTrace(0, logCatgory, TraceSeverity.Medium, className + "::" + methodName + "::" + message);
                    break;                
                case LogCategories.MY_APPLICATION_WARNING:
                    LoggingService.LogSerivce.WriteTrace(0, logCatgory, TraceSeverity.High, className + "::" + methodName + "::" + message);
                    break;                
                case LogCategories.MY_APPLICATION_ERROR:
                    LoggingService.LogSerivce.WriteTrace(0, logCatgory, TraceSeverity.Unexpected, className + "::" + methodName + "::" + message);
                    break;
            }
        }
    }
}
